#定义加载头
Author = 'HuoHuaX'
Type = 'official'

#定义加载器方法
from index import *
import sqlite3 as sq
cmds = {}
class Cmd():
    def __init__(self,ctype:str,command:str,func):
        self.type = ctype
        self.command = command
        self.function = func

#注册一个botcmd
def regBotCmd(cmd:str,function) -> dict:
    '''注册一个普通命令
    :param cmd: 需要注册的命令
    :param function: 回调函数,(不需要括号)
    '''
    global cmds
    if cmd not in cmds:
        cmds[cmd] = Cmd('ordinary',cmd,function)
        log_debug(cmds)
        return {'code':0,'msg':'register cmd success'}
    else:
        return {'code':2,'msg':'This command is registered'}

def regArgCmd(cmd_m:str,function) -> dict:
    '''注册一个参数命令
    :param cmd_m: 需要注册的命令关键字
    :param function: 回调函数,(不需要括号)
    '''
    global cmds
    if cmd_m not in cmds:
        cmds[cmd_m] = Cmd('args',cmd_m,function)
        return {'code':0,'msg':'register cmd success'}
    else:
        return {'code':2,'msg':'This command is registered'}

#向所有群发送消息
def SendAllGroup(text:str) -> dict:
    '''向所有群发消息
    :param text: 发送的文本
    '''
    for i in config['Group']:
        sendGroupMsg(i,text)
    return {'code':0,'msg':'Send message success'}

#指定群发消息
def SendGroup(group:int,text:str):
    '''向指定群发消息
    :param group: 指定的群号
    :param text: 发送的文本
    '''
    if group in config['Group']:
        sendGroupMsg(group,text)
        return {'code':0,'msg':'Send message success'}
    else:
        return {'code':1,'msg':'Not Found Group'}

#使用QQ号获取Xboxid
def getXboxID(qqid) -> str:
    '''使用QQ号获取xboxid
    :param qqid: QQ号
    注：未绑定为%Xboxid%
    '''
    conn = sq.connect('data/xuid.db')
    c = conn.cursor()
    cursor = c.execute("SELECT *  from xboxid")
    name = r'%Xboxid%'
    for row in cursor:
        qq = row[0]
        xboxid = row[1]
        if qqid == qq:
            name = xboxid
    conn.close()
    return name

def runcmd(cmd:str) -> dict:
    '''运行bds命令
    :param cmd: 命令
    '''
    try:
        global NormalStop
        result=cmd+'\r\n'
        if result.encode('utf-8') == b'stop\r\n':
            return {'code':3,'msg':'Cannot run the stop command'}
        else:
            if check(obj):
                obj.stdin.write(result.encode('utf-8'))
                obj.stdin.flush()
                return {'code':0,'msg':'Run command success'}
            else:
                return {'code':5,'msg':'BDS is not running'}
    except Exception as e:
        log_debug(e)
        return {'code':4,'msg':'Cannot run command'}


#加载的插件
from PPlugin import loadlist
for i in loadlist:
    log_info(PLP['Plugin.loading'].replace('%plugin%',i))
from PPlugin import *